package com.dance.netty.nio.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 使用通道实现图片拷贝
 */
public class FileChannel04 {

    public static void main(String[] args) throws Exception {

        FileInputStream fileInputStream = new FileInputStream("src/main/resources/01.jpeg");
        FileOutputStream fileOutputStream = new FileOutputStream("src/main/resources/02.jpeg");

        FileChannel sourceChannel = fileInputStream.getChannel();
        FileChannel targetChannel = fileOutputStream.getChannel();

        // 将源Channel的数据直接传输到目标Channel 从0开始 到 源的大小
        long length = targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());

        System.out.println(length);

        targetChannel.close();
        sourceChannel.close();
        fileOutputStream.close();
        fileInputStream.close();

    }

}
